home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / _archvrs / unix / zoo21src.lha / zoo / zooadd.c < prev    next >
C/C++ Source or Header  |  1992-11-20  |  27KB  |  735 lines

  1. #ifndef LINT
  2. /* derived from: zooadd.c 2.34 88/08/15 10:53:11 */
  3. static char sccsid[]="$Source: g:/newzoo\RCS\zooadd.c,v $\n\
  4. $Id: zooadd.c,v 1.3 1991/07/24 23:47:04 bjsjr Rel $";
  5. #endif /* LINT */
  6.  
  7. /*
  8. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  9. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  10. (C) Copyright 1991 Rahul Dhesi -- All rights reserved
  11. */
  12. #include "options.h"
  13. /* Adds files specified in parameter-list to archive zoo_path. */
  14.  
  15. #define LONGEST    20                    /* assumed length of longest filename */
  16. #include "zoomem.h"             /* to define MAXADD */
  17. #include "zoo.h"
  18. #include "zooio.h"
  19. #include "various.h"
  20. #include "parse.h"
  21. #include "debug.h"
  22.  
  23. #include "portable.h"
  24.  
  25. #include "zoofns.h"
  26. #include "errors.i"
  27. extern int break_hit;
  28. extern int quiet;
  29.  
  30. void show_comment PARMS ((struct direntry *, ZOOFILE, int, char *));
  31. void dosname PARMS ((char *, char *));
  32. void modpath PARMS ((char *));
  33. void opts_add PARMS ((char *, int *, int *, int *, int *, int *, int *,
  34.                int *, int *, int *, int *, int *, int *, int *, int *));
  35. int ver_too_high PARMS ((struct zoo_header *));
  36. void get_comment PARMS ((struct direntry *, ZOOFILE, char *));
  37. #ifndef PORTABLE
  38. void copyfields PARMS ((struct direntry *, struct tiny_header *));
  39. #endif
  40. void storefname PARMS ((struct direntry *, char *, int));
  41. char *choosefname PARMS ((struct direntry *));
  42.  
  43. extern struct zoo_header zoo_header;
  44.  
  45. extern char file_leader[];
  46. extern unsigned int crccode;
  47.  
  48. void zooadd(zoo_path, argc, argv, option)
  49. char *zoo_path;      /* pathname of zoo archive to add to */
  50. int argc;            /* how many filespecs supplied */
  51. char **argv;         /* array of pointers to filespecs */
  52. char *option;        /* option string */
  53. {
  54. char *whichname;                          /* which name to show user */
  55. char **flist;                                  /* list of ptrs to input fnames */
  56. int fptr;                                 /* will point to within flist */
  57. ZOOFILE this_file;                        /* file to add */
  58. char zoo_fname[LFNAMESIZE];               /* basename of archive itself */
  59. char zoo_bak[LFNAMESIZE];                 /* name of archive's backup */
  60. char this_fname[LFNAMESIZE];              /* just filename of file to add */
  61. char latest_name[LFNAMESIZE];             /* latest name in archive */
  62. long last_old = 0L;                       /* last direntry in old chain */
  63. ZOOFILE zoo_file;                         /* stream for open archive */
  64. char *this_path;                          /* pathname of file to add */
  65.  
  66. #ifdef NOENUM
  67. #define NEW_ZOO 1
  68. #define OLD_ZOO 2
  69. int zoo_status;
  70. #else
  71. enum {NEW_ZOO, OLD_ZOO} zoo_status;       /* newly created or not */
  72. #endif
  73.  
  74. long this_dir_offset;                     /* pointers to within archive */
  75. long save_position;                       /* pointer to within archive */
  76. long prev_pos;                            /* posn of prev file of same name */
  77. struct direntry direntry;                 /* directory entry */
  78. struct direntry dir2entry;                /* spare */
  79. int status;                               /* error status */
  80. int success;                              /* successful addition of file? */
  81. int addcount = 0;                         /* number added */
  82. int update=0;                             /* only files already in archive */
  83. int suppress=0;                           /* suppress compression */
  84. int new=0;                                /* add only files not in archive */
  85. int zootime = 0;                          /* just set archive time */
  86. int add_comment = 0;                      /* add comment */
  87. int add_global_comment = 0;                    /* archive comment */
  88. int pack = 0;                             /* pack after adding */
  89. int need_dir = 1;                         /* store directories too */
  90. int delcount = 0;                         /* count of deleted entries */
  91. int exit_status = 0;                      /* exit status to set */
  92.  
  93. unsigned int latest_date = 0;             /* to set time on archive itself */
  94. unsigned int latest_time = 0;             /* .. same */
  95. int move = 0;                             /* delete after adding to archive */
  96. int longest;                              /* length of longest pathname added */
  97. int firstfile = 1;                        /* first file being added? */
  98. int z_fmt = 0;                            /* look for Z format files? */
  99. int inargs = 0;                                    /* read filenames from stdin? */
  100.  
  101. #ifndef PORTABLE
  102. struct tiny_header tiny_header;           /* for Z format archives */
  103. #endif
  104.  
  105. unsigned this_version_no;                            /* version no. of old file */
  106. unsigned  high_vflag;                                /* version flag of old file */
  107. unsigned high_version_no;                            /* highest version no of this file */
  108. long high_pos;                                        /* offset of file w/highest ver no */
  109. unsigned int fgens;                                /* gens. to preserve -- file */
  110. unsigned int zgens;                                /* gens. to preserve -- archive */
  111. long oldcmtpos;                                    /* to save old comment */
  112. unsigned int oldcmtsiz;                            /* to save old comment */
  113. int genson = 0;                                    /* whether to turn generations on */
  114.  
  115. int use_lzh = 0;                                    /* whether to use lzh compression */
  116.  
  117. /* on entry option points to first letter */
  118.  
  119. opts_add (option, &zootime, &quiet, &suppress, &move, &new, &pack,
  120.           &update, &add_comment, &z_fmt, &need_dir, &inargs, &genson,
  121.              &use_lzh, &add_global_comment);
  122.  
  123. /* POSSIBLE RACE CONDITION BETWEEN TESTING EXISTENCE AND CREATING FILE */
  124. if (exists (zoo_path)) {
  125.     zoo_file = zooopen (zoo_path, Z_RDWR);
  126.    zoo_status = OLD_ZOO;
  127. } else {
  128.    if (!zootime)
  129.         zoo_file = zoocreate (zoo_path);
  130.    else
  131.       zoo_file = NOFILE;     /* don't create if just setting time */
  132.    zoo_status = NEW_ZOO;
  133. }
  134.  
  135. if (zoo_file == NOFILE)
  136.    prterror ('f', could_not_open, zoo_path);
  137. basename(zoo_path, zoo_fname);      /* get basename of archive */
  138. rootname (zoo_path, zoo_bak);       /* name without extension */
  139. strcat (zoo_bak, BACKUP_EXT);       /* name of backup of this archive */
  140.  
  141. /* Now we prepare the archive for adding one or more files.  If the archive
  142. has just been created, we write the archive header */
  143.  
  144. addfname ("",0L,0,0,0,0); /* initialize table of files already in archive */
  145. if (zoo_status == NEW_ZOO) {                 /* newly-created archive */
  146.     if (genson)                                            /* if no generations needed */
  147.         zoo_header.vdata = (VFL_ON|GEN_DEFAULT); /* generations on */
  148.    fwr_zooh (&zoo_header, zoo_file);
  149.     zgens = GEN_DEFAULT;
  150.    zooseek (zoo_file, zoo_header.zoo_start, 0); /* seek to where data begins */
  151. } else {
  152.    /* read header and rewrite with updated version numbers, preserving
  153.        header type */
  154.    rwheader (&zoo_header, zoo_file, 1);
  155.     zgens = zoo_header.vdata & VFL_GEN;            /* get archive generations */
  156.    /* initialize latest_name to null string */
  157.     /* NOTE:  latest_name is not currently used for anything, but
  158.         may be used in the future for inserting files into the
  159.         archive in alphabetic order. */
  160.    *latest_name = '\0';
  161.  
  162.    /* Skip existing files but add them to a list.  The variable last_old 
  163.    gets the tail of the old chain of directory entries */
  164.    skip_files (zoo_file, &latest_date, &latest_time, &delcount, 
  165.                latest_name, &last_old);
  166. }
  167.  
  168. /* The file pointer is now positioned correctly to add a file to archive, 
  169. unless the null directory entry is too short.  This will be fixed below. */
  170.  
  171. /* If we are just setting time, do it and run. */
  172. if (zootime) {
  173. #ifdef NIXTIME
  174.    zooclose (zoo_file);
  175.    setutime (zoo_path, latest_date, latest_time);
  176. #else
  177.    settime (zoo_file, latest_date, latest_time);
  178.    zooclose (zoo_file);
  179. #endif
  180.    prterror ('m', "Archive time adjusted.\n");
  181.    zooexit (0);
  182. }
  183.  
  184. /* make list of files, excluding archive and its backup */
  185. longest = LONGEST;
  186. flist = (char **) ealloc(MAXADD);
  187.  
  188. if (!inargs) {
  189.    makelist(argc, argv, flist, MAXADD-2, zoo_fname, zoo_bak, ".", &longest);
  190.    /*                                    ^^         ^^       ^^ exclude */
  191. }
  192.  
  193. fptr = 0;    /* ready to get filename (if makelist() was called) or to
  194.                     begin adding filenames (if reading them from stdin) */
  195.  
  196. while (1) {
  197.    unsigned int this_date, this_time;
  198.    int INLIST; /* boolean */
  199.    int RECENT; /* boolean */
  200.    int danger; /* if update requested and disk copy is out of date */
  201.     if (inargs) {
  202.     again: /* loop back if filename was same as archive name or its backup */
  203.         this_path = getstdin();            /* pathname from stdin, in static area */
  204.         if (this_path != NULL) {
  205.             if (samefile (nameptr(zoo_fname),nameptr(this_path)) ||
  206.                         samefile (nameptr(zoo_bak),nameptr(this_path)))
  207.                 goto again;                 /* don't add archive to itself */
  208.             modpath (this_path);
  209.         /* if moving files, add to list for later deletion;  if list overflows,
  210.             terminate addition loop and give warning message */
  211.             if (move) {
  212.                 if (fptr >= MAXADD-2) {
  213.                     prterror ('w', too_many_files, MAXADD-2);
  214.                     this_path = NULL;
  215.                 } else
  216.                     flist[fptr++] = str_dup (this_path);
  217.             }
  218.         }
  219.     } else  {
  220.         this_path = flist[fptr++];
  221.     }
  222.     /* exit the addition loop when no more pathnames are left */
  223.     if (this_path == NULL) {
  224.         /* in case stdin was being read, make sure flist is NULL-terminated */
  225.         flist[fptr] = NULL;
  226.         break;
  227.     }
  228.  
  229.    basename (this_path, this_fname);   /* get just filename for later */
  230.  
  231.    this_file = zooopen(this_path, Z_READ);
  232.    if (this_file == NOFILE) {
  233.       prterror ('e', could_not_open, this_path);
  234.       exit_status++;
  235.       continue;
  236.    }
  237.  
  238. #ifndef PORTABLE
  239.    /* Test to see if this is a Z format file.  We assume the file is Z format
  240.       if (a) tag is correct and (b) type is 1 and (c) embedded filename
  241.       is not longer than FNAMESIZE.  
  242.    */
  243.    if (z_fmt) {
  244.       zooread (this_file, (char *) &tiny_header, sizeof(tiny_header));
  245.       if (tiny_header.tinytag == TINYTAG && tiny_header.type == 1 &&
  246.                         strlen (tiny_header.fname) <= FNAMESIZE)
  247.           /* ok */ ;
  248.       else {
  249.          zooclose (this_file);
  250.          prterror ('e', "File %s does not have Z format.\n", this_fname);
  251.          exit_status++;
  252.          continue;
  253.       }
  254.    }
  255. #endif
  256.  
  257.    /* get file time;  also fix name */
  258. #ifndef PORTABLE
  259.    if (z_fmt) {
  260.       direntry.date = tiny_header.date;
  261.       direntry.time = tiny_header.time;
  262.       strcpy (direntry.fname, tiny_header.fname);
  263.       direntry.dirlen = direntry.namlen = 0;
  264.    } else {
  265. #endif
  266.  
  267.       /* Get timstamp of file being added */
  268. #ifdef GETUTIME
  269.       getutime (this_path, &direntry.date, &direntry.time);
  270. #else
  271.       gettime (this_file, &direntry.date, &direntry.time);
  272. #endif
  273.  
  274.         /* save file attributes */
  275. #ifdef FATTR
  276.         /* we expect getfattr() to set all attr. bits;  currently
  277.             only the portable format is recognized */
  278.         {
  279. # ifdef FATTR_FNAME
  280.             unsigned long getfattr PARMS ((char *);
  281.             direntry.fattr = getfattr (this_path);
  282. # else
  283.             unsigned long getfattr PARMS ((ZOOFILE));
  284.             direntry.fattr = getfattr (this_file);
  285. # endif /* FATTR_FNAME */
  286.         }
  287. #else
  288.             direntry.fattr = NO_FATTR;    /* none */
  289. #endif /* FATTR */
  290.  
  291. #ifdef FOLD
  292.       str_lwr(this_fname);
  293. #endif
  294.       dosname (this_fname, direntry.fname);  /* MSDOS filename */
  295.  
  296.    /*
  297.    Store long filename into direntry.lfname iff it is different from MSDOS
  298.    filename.  Also store directory name if need_dir is true.  Moved out of 
  299.    zooadd() so zooadd() doesn't get too big for optimization.
  300.    */
  301.    storefname (&direntry, this_path, need_dir);
  302.  
  303. #ifndef PORTABLE
  304.    }
  305. #endif
  306.  
  307. #ifdef DEBUG
  308. printf ("zooadd:  direntry.lfname = [%s]  direntry.dirname = [%s]\n",
  309.                   direntry.lfname, direntry.dirname);
  310. #endif
  311.  
  312.    /* if update option, then we add file if it is already in the archive 
  313.       AND the archived file is older */
  314.  
  315.    /* The following logic was derived from a Karnaugh map so it may
  316.       be hard to understand.  Essentially, if U=update requested,
  317.       N=new files requested, I=file is already in archive, and
  318.       R=file being archived is more recent than file already in
  319.       archive, then the boolean equation is:
  320.  
  321.       add = U' (N' + I') + U (IR  + I'N)
  322.    */
  323.  
  324.    /* Get the filename to use for this addition.  */
  325.    whichname = choosefname(&direntry);
  326.  
  327.    /* Get position in archive of any old file of same name, ignoring
  328.         any directory prefix if need_dir is not true.  Also get its
  329.         date, time, version flag, and version number. */
  330.    prev_pos = inlist (fullpath (&direntry), &this_date, &this_time, 
  331.                 &this_version_no, &high_vflag, &high_version_no, 
  332.                 &high_pos, !need_dir);
  333.  
  334. /* define DBG_INLIST for debugging by printing values returned by inlist() */
  335. #ifdef DBG_INLIST
  336.     printf ("FROM inlist(): prev_pos=%ld, high_pos=%ld\n", prev_pos, high_pos);
  337.     printf ("this_version_no=%u, high_vflag=%4x, high_version_no=%u\n",
  338.                 this_version_no,    high_vflag,     high_version_no);
  339. #endif
  340.  
  341.    INLIST = prev_pos > 0;  /* already in archive if positive value */
  342.    if (INLIST) {
  343.       int result;
  344.       result = cmpnum (direntry.date, direntry.time, this_date, this_time);
  345.       RECENT = result > 0;
  346.       danger = result < 0;
  347.    } else
  348.       danger = 0; /* And RECENT is undefined and should not be used */
  349.  
  350.    if (
  351.          !update && (!new || !INLIST) ||
  352.          update && (INLIST && RECENT || !INLIST && new)
  353.       )
  354.          ;  /* then continue and add file */
  355.    else {
  356.       if (update && danger)
  357.          prterror ('w', "Archived copy of %s is newer.\n", whichname);
  358.       zooclose (this_file);
  359.       continue;   /* cycle back, skip this file */
  360.    }
  361.  
  362. #ifdef CHEKDIR
  363.    /* Don't add if this is a directory */
  364.    if (isadir (this_file)) {
  365.       zooclose (this_file);
  366.       continue;
  367.    }
  368. #else
  369. # ifdef CHEKUDIR
  370.    /* Don't add if this is a directory */
  371.    if (isuadir (this_path)) {
  372.       zooclose (this_file);
  373.       continue;
  374.    }
  375. # endif /* CHEKUDIR */
  376. #endif /* CHEKDIR */
  377.  
  378.    /* Create directory entry for new file (but don't add just yet) */
  379.    /* NOTE:  we already got file date and time above for update option */
  380.     /* add tag, type, timezone, struc, system_id, and var_dir_len */
  381.     newdir (&direntry);
  382.  
  383.     if (!genson && zoo_status == NEW_ZOO || 
  384.                 (zoo_header.vdata & VFL_ON) == 0) {
  385.         direntry.vflag = 0;
  386.         direntry.version_no = 0;
  387.     }
  388.  
  389.    /* 
  390.    Write a null direntry entry.  Thus, if an error occurs or the program
  391.    is interrupted, the end of the archive will still be meaningful.
  392.    Special check needed for first one written.
  393.    */
  394.  
  395.    direntry.next = direntry.offset = 0L;     /* trailing null entry */
  396.    this_dir_offset = zootell (zoo_file);
  397.    if (!firstfile) {
  398.       writedir (&direntry, zoo_file);
  399.    } else {
  400.       /*
  401.       Before adding the first file to the archive, we must make sure that
  402.       the previous directory chain (if any) is properly terminated with a
  403.       null entry of the right size.  If this is a new archive, we simply
  404.       write a new null entry of the right size.  If this is an existing
  405.       archive, we must check the size of the previous trailing null entry. 
  406.       If it is too small, we will back up to the most recent real directory
  407.       entry and change its .next field to point to end of file.  
  408.       */
  409.  
  410.       if (zoo_status == NEW_ZOO) {
  411.          writedir (&direntry, zoo_file);        /* write null dir entry */
  412.       } else {
  413.          struct direntry tmpentry;
  414.          long tmppos;
  415.          int oldlen, newlen;
  416.          tmppos = zootell (zoo_file);
  417.          frd_dir (&tmpentry, zoo_file);
  418. #define  DIRLEN(x)   ((x.type<2) ? SIZ_DIR : (SIZ_DIRL+x.var_dir_len))
  419.          oldlen = DIRLEN(tmpentry);             /* get length of direntry */
  420.          newlen = DIRLEN(direntry);             /* ditto */
  421.  
  422.          if (newlen > oldlen) {                 /* trouble */
  423.             zooseek (zoo_file, last_old, 0);    /* back to previous entry */
  424.             frd_dir (&tmpentry, zoo_file);
  425.             zooseek (zoo_file, 0L, 2);          /* get EOF position */
  426.             tmpentry.next = zootell (zoo_file);    /* point to EOF */
  427.             zooseek (zoo_file, last_old, 0);    /* back to previous entry */
  428.             writedir (&tmpentry, zoo_file);     /* update it */
  429.             zooseek (zoo_file, 0L, 2);          /* to EOF ... */
  430.             this_dir_offset = zootell (zoo_file);
  431.             writedir (&direntry, zoo_file);     /* ...write null dir entry */
  432.          } else
  433.             zooseek (zoo_file, tmppos, 0);      /* long enough -- let it be */
  434.       } /* if (zoo_status == NEW_ZOO) ... */
  435.    } /* if (!firstfile) ... */
  436.  
  437.    /* Now `this_dir_offset' is where the next directory entry will go */
  438.  
  439.    /* first file added goes at EOF to avoid overwriting comments */
  440.    if (firstfile) {
  441.       zooseek (zoo_file, 0L, 2);                   /* EOF */
  442.       direntry.offset = zootell (zoo_file) + SIZ_FLDR;
  443.    } else {
  444.       direntry.offset = this_dir_offset + SIZ_DIRL + 
  445.          direntry.var_dir_len + SIZ_FLDR;
  446.    }
  447.  
  448.     if (use_lzh) {
  449.         direntry.major_ver = MAJOR_LZH_VER;    /* minimum version number needed */
  450.         direntry.minor_ver = MINOR_LZH_VER;    /* .. to extract */
  451.     } else {
  452.         direntry.major_ver = MAJOR_EXT_VER;    /* minimum version number needed */
  453.         direntry.minor_ver = MINOR_EXT_VER;    /* .. to extract */
  454.     }
  455.    direntry.deleted = 0;               /* not deleted, naturally */
  456.    direntry.comment = 0L;              /* no comment (yet) */
  457.    direntry.cmt_size = 0;          /* .. so no size either */
  458.  
  459.    save_position = direntry.offset;          /* save position in case of error */
  460.  
  461.    (void) zooseek (zoo_file, direntry.offset - SIZ_FLDR, 0);
  462.    (void) zoowrite (zoo_file, file_leader, SIZ_FLDR);
  463.  
  464. #ifdef PORTABLE
  465.    prterror ('m', "%-*s -- ", longest, this_path);
  466. #else
  467.    if (z_fmt)
  468.       prterror ('m', "%-12s <== %-*s -- ", 
  469.          direntry.fname, longest, this_path);
  470.    else
  471.       prterror ('m', "%-*s -- ", longest, this_path);
  472.  
  473. #endif /* PORTABLE */
  474.  
  475.    crccode = 0;
  476. #ifndef PORTABLE
  477.    if (z_fmt) 
  478.     {
  479.       direntry.packing_method = tiny_header.packing_method;
  480.       zooseek (this_file, (long) (sizeof(tiny_header)+tiny_header.cmt_size), 0);
  481.       status = getfile (this_file, zoo_file, tiny_header.size_now, 1);
  482.    } else 
  483. #endif
  484.     if (suppress) {                    /* suppress compression */
  485.       direntry.packing_method = 0;           /* no compression */
  486.       status = getfile (this_file, zoo_file, -1L, 1);
  487.    } else {
  488. #ifdef UNBUF_IO    /* unbuffered I/O */
  489.         long lseek PARMS ((int, long, int));
  490.         long tell PARMS ((int));
  491.         int this_fd, zoo_fd;
  492. #endif
  493.         if (use_lzh)
  494.             direntry.packing_method = 2;
  495.         else
  496.             direntry.packing_method = 1;
  497. #ifdef UNBUF_IO
  498. #include "UNBUF_IO not currently supported"
  499.         this_fd = fileno (this_file);                        /* get ..                    */
  500.         zoo_fd = fileno (zoo_file);                        /* ... file descriptors    */
  501.         zooseek (zoo_file, zootell (zoo_file), 0);    /* synch */
  502.         zooseek (this_file, zootell (this_file), 0);    /* synch */
  503.       status = lzc(this_fd, zoo_fd);                    /* add with compression */
  504.         zooseek (zoo_file, tell (zoo_fd), 0);            /* resynch    */
  505.         zooseek (this_file, tell (this_fd), 0);        /* resynch    */
  506. #else
  507.         if (use_lzh)
  508.             status = lzh_encode(this_file, zoo_file);
  509.         else
  510.             status = lzc(this_file, zoo_file);
  511. #endif /* UNBUF_IO */
  512.  
  513.    }
  514.    if (status != 0) { /* if I */
  515.       ++exit_status;                         /* remember error */
  516.       if (status == 1)
  517.          prterror ('F', no_memory);
  518.       else if (status == 2)
  519.          prterror ('F', disk_full);
  520.       else if (status == 3)
  521.          prterror ('F', "Read error.\n");
  522.       else
  523.          prterror ('F', internal_error);
  524.       success = 0;
  525.    } else {
  526.       direntry.next  = zootell (zoo_file);
  527.       direntry.size_now = direntry.next - direntry.offset;
  528.  
  529.       /* find and store original size of file just compressed */
  530. /*DEBUG VMS*/ zooseek (this_file, 0L, 2);    /* seek to EOF */
  531.  
  532.       direntry.org_size = zootell (this_file);  /* should be EOF already */
  533.  
  534.       /* If the compressed one is bigger, just copy */
  535.  
  536.       if (direntry.size_now >= direntry.org_size &&   /* if II */
  537.             direntry.packing_method != 0) {
  538.          zooseek (zoo_file, save_position, 0);  /* ..restore file pointer */
  539.          zootrunc (zoo_file);                   /* ..truncate file */
  540.          direntry.packing_method = 0;           /* ..and just copy */
  541.          zooseek (this_file, 0L, 0);            /* (but rewind first!) */
  542.          crccode = 0;                           /* re-start crc from 0 */
  543.          status = getfile (this_file, zoo_file, -1L, 1);
  544.          if (status != 0) {  /* if III */
  545.             success = 0;
  546.             printf (disk_full);
  547.             exit_status++;
  548.          } else {
  549.             success = 1;
  550.             direntry.next  = zootell (zoo_file);
  551.             direntry.size_now = direntry.next - direntry.offset;
  552.          } /* end if III */
  553.       } else {
  554.          success = 1;
  555.       } /* end if II */
  556.  
  557.    } /* end if I */
  558.  
  559.    if (success) {                               /* file successfully added */
  560.       addcount++;                               /* how many added */
  561.       direntry.file_crc = crccode;
  562.  
  563.       /* remember most recent date and time */
  564.       if (cmpnum (direntry.date,direntry.time,latest_date,latest_time) > 0) {
  565.             latest_date = direntry.date;
  566.             latest_time = direntry.time;
  567.       }
  568.  
  569. #if 0
  570.       /* mark any previous version of this file in archive as deleted */
  571.       dir2entry.comment = 0L;       /* for later use assigning to direntry */
  572.       dir2entry.cmt_size = 0;
  573. #endif
  574.  
  575.       if (!z_fmt)
  576.          prterror ('M', " (%2d%%) ", cfactor (direntry.org_size, direntry.size_now));
  577.  
  578.         oldcmtsiz = 0;                                /* assume no old comment */
  579.         oldcmtpos = 0L;
  580.  
  581.       if (prev_pos > 0) {                                        /* in archive */
  582.             int delold = 0;                                        /* delete old? */
  583.             /* if versions active both archive-wide and for file */
  584.             if ((zoo_header.vdata & VFL_ON) && (high_vflag & VFL_ON)) {
  585.                 /* next test is optimization, to avoid redundant I/O */
  586.                 if (high_pos != prev_pos || this_version_no == 1) {
  587.                     /* prev highest is no longer highest so adjust vflag */
  588.                     long save_pos = zootell (zoo_file);            /*DEBUG*/
  589.                     zooseek (zoo_file, high_pos, 0);
  590. #ifndef GLOB
  591.                            readdir (&dir2entry, zoo_file, 1);
  592. #else
  593.                            zreaddir (&dir2entry, zoo_file, 1);
  594. #endif GLOB
  595.                     oldcmtpos = dir2entry.comment;
  596.                     oldcmtsiz = dir2entry.cmt_size;
  597.                     dir2entry.vflag &= (~VFL_LAST);                /* no longer highest */
  598.                     zooseek (zoo_file, high_pos, 0);
  599.                     writedir (&dir2entry, zoo_file);
  600.                     zooseek (zoo_file, save_pos, 0);                /*DEBUG*/
  601.                 }
  602.  
  603.                 direntry.version_no = high_version_no + 1; /* ..one higher */
  604.                 direntry.vflag = high_vflag;
  605.                 /* now see if we need to delete older version */
  606.                 fgens = high_vflag & VFL_GEN;
  607.                 if (fgens == 0)
  608.                     fgens = zgens;
  609.                 if (zgens != 0 && zgens < fgens)
  610.                     fgens = zgens;
  611.                 if (fgens != 0 && direntry.version_no - this_version_no >= fgens) {
  612.                     delold = 1;
  613.                     prterror ('M', "replaced+\n");
  614.                 } else
  615.                     prterror ('M', "added+\n");
  616.             } else {
  617.                 prterror ('M', "replaced\n");
  618.                 delold = 1;
  619.             }
  620.  
  621.             if (delold) {                                            /* deleting old file */
  622.                 long save_pos = zootell (zoo_file);            /*DEBUG*/
  623.                 ++delcount;                                            /* remember to pack */
  624.                 zooseek (zoo_file, prev_pos, 0);
  625. #ifndef GLOB
  626.                 readdir (&dir2entry, zoo_file, 1);
  627. #else
  628.                 zreaddir (&dir2entry, zoo_file, 1);
  629. #endif /* GLOB */
  630.                 if (dir2entry.cmt_size != 0) {        /* propagate latest comment */
  631.                     oldcmtpos = dir2entry.comment;
  632.                     oldcmtsiz = dir2entry.cmt_size;
  633.                 }
  634.                 dir2entry.deleted = 1;                            /* mark as deleted */
  635.                 /* following line is optimization if only 1 generation */
  636.                 dir2entry.vflag &= (~VFL_LAST);                /* no longer highest */
  637.                 zooseek (zoo_file, prev_pos, 0);
  638.                 writedir (&dir2entry, zoo_file);
  639.                 zooseek (zoo_file, save_pos, 0);                /*DEBUG*/
  640.             }
  641.       } else                                                         /* not in archive */
  642.             prterror ('M', "added\n");
  643.  
  644.       /* Preserve any old comment if we replaced or superseded the file */
  645.       direntry.comment = oldcmtpos;
  646.       direntry.cmt_size = oldcmtsiz;
  647.  
  648. #ifndef PORTABLE
  649.       /* Copy comment if any from Z format file */
  650.       if (z_fmt && tiny_header.cmt_size != 0) {
  651.          zooseek (this_file, (long) sizeof(tiny_header), 0); /* to comment */
  652.          direntry.comment = zootell (zoo_file);
  653.          direntry.cmt_size = tiny_header.cmt_size;
  654.          /* 4th param is 0 for no CRC */
  655.          getfile (this_file, zoo_file, (long) tiny_header.cmt_size, 0);
  656.          direntry.next = zootell (zoo_file);
  657.       } 
  658. #endif
  659.  
  660.       /* if user requested comments, any previous comment in a Z format
  661.          file may now be manually overwritten */
  662.       if (add_comment && !feof (stdin)) {
  663.          show_comment (&direntry, zoo_file, 1, whichname);
  664.          get_comment (&direntry, zoo_file, this_path);
  665.          direntry.next = zootell (zoo_file);    /* update .next ptr */
  666.       } /* end if */
  667.  
  668. #ifndef PORTABLE
  669.       /* if adding Z format archive, copy relevant fields from its header */
  670.       if (z_fmt) {   /* moved out to shorten code & allow optimizer to work */
  671.          copyfields (&direntry, &tiny_header);
  672.       }
  673. #endif
  674.  
  675.       debug((printf ("zooadd:  our new .next = [%lx].\n", direntry.next)))
  676.  
  677.       {
  678.          long savepos = zootell (zoo_file);    /* save position */
  679.          zooseek (zoo_file, this_dir_offset, 0);
  680.          writedir (&direntry, zoo_file);
  681.          zooseek (zoo_file, savepos, 0);    /* restore position */
  682.       }
  683.  
  684.    } else {                               /* file was not properly added */
  685.       zooseek (zoo_file, save_position, 0);  /* ..restore file pointer */
  686.       zootrunc (zoo_file);                   /* ..truncate file */
  687.    } /* end if */
  688.    zooclose (this_file);
  689. if (!success)
  690.    break;
  691. firstfile = 0;
  692. } /* end for */
  693.  
  694. save_position = zootell (zoo_file);
  695.  
  696. /* Write a null direntry entry */
  697. zooseek (zoo_file, save_position, 0);
  698. writenull (zoo_file, MAXDIRSIZE);
  699. zootrunc (zoo_file);  /* truncate */
  700.  
  701. #ifdef NIXTIME
  702. zooclose (zoo_file);
  703. setutime (zoo_path, latest_date, latest_time);
  704. #else
  705. settime (zoo_file, latest_date, latest_time);
  706. zooclose (zoo_file);
  707. #endif
  708.  
  709. if (!addcount) {                    /* no files added */
  710.    prterror ('m', "No files added.\n");
  711.    if (zoo_status == NEW_ZOO)
  712.       unlink (zoo_path);
  713. } else {
  714.    if (delcount && pack) { /* pack if user asked and found deleted entries */
  715.       prterror ('M', "-----\nPacking...");
  716.       zoopack (zoo_path, "PP");
  717.       prterror ('M', "done\n");
  718.    }
  719.  
  720.    /* If files to move & we added some and no error so far, delete originals */
  721.    if (move && !exit_status)
  722.       if (kill_files (flist, longest) != 0)
  723.          exit_status++;
  724. }
  725.  
  726. /* right here we handle archive comment */
  727. if (add_global_comment) {
  728.     comment(zoo_path, "_A");
  729.     add_global_comment = 0;
  730. }
  731.  
  732. if (exit_status)
  733.    zooexit (1);
  734. } /* end zoo_add */
  735.